
let XuPromise = function(callback) {
    // 定义状态
    this.status = 'pending';
    // 定义resolve任务队列
    this.resolveArr = [];
    // 定义reject任务队列
    this.rejectArr = [];

    let resolve = (value) => {
        // 缓存value
        this.doneValue = value;

        this.status = 'resolved';
        // 执行resolve任务队列
        this.resolveArr.forEach(fn => value = fn(value))
    }

    let reject = (value) => {
        // 缓存value
        this.doneValue = value;

        this.status = 'rejected';
        // 执行reject任务队列
        this.rejectArr.forEach(fn => value = fn(value));
    }

    try {

        // 执行callback;
        callback(resolve, reject);
    }catch (err){
        reject(err);
    }   
    
}
XuPromise.prototype.then = function(success, feil) {
    // 判断状态的执行方法
    if(this.status === 'pending') {
        // 添加任务队列
        typeof success === 'function' && this.resolveArr.push(success);
        typeof feil === 'function' && this.rejectArr.push(feil);
    }else if(this.status === 'resolve') { 
        // 立即 执行
        typeof success === 'function' && success(this.doneValue);
    }else {
        typeof feil === 'function' && feil(this.doneValue);
    }
    return this;
}